﻿namespace Utility
{
    public class Motor
    {
        private int IN1 { get; set; }
        private int IN2 { get; set; }
        private int IN3 { get; set; }
        private int IN4 { get; set; }
        private int ENA { get; set; }
        private int ENB { get; set; }
        public Motor()
        {
            this.IN1 = 5;
            this.IN2 = 6;
            this.IN3 = 13;
            this.IN4 = 19;
            this.ENA = 21;
            this.ENB = 26;

            Init();
        }

        public Motor(int IN1, int IN2, int IN3, int IN4, int ENA, int ENB)
        {
            this.IN1 = IN1;
            this.IN2 = IN2;
            this.IN3 = IN3;
            this.IN4 = IN4;
            this.ENA = ENA;
            this.ENB = ENB;
            Init();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns>是否成功</returns>
        public bool Init()
        {
            //初始化为BCM模式
            bool bl = GPIO.WiringPiSetupGpio() != -1;

            if (!bl)
            {
                return bl;
            }
            //设置IO模式
            GPIO.PinMode(IN1, WiringPi.Output);
            GPIO.PinMode(IN2, WiringPi.Output);
            GPIO.PinMode(IN3, WiringPi.Output);
            GPIO.PinMode(IN4, WiringPi.Output);

            //PWM
            GPIO.SoftPwmCreate(ENA, 100, 1024);
            GPIO.SoftPwmCreate(ENB, 100, 1024);
            return bl;
        }
        /// <summary>
        /// 前进
        /// </summary>
        public void Up()
        {
            GPIO.DigitalWrite(IN1, WiringPi.High);
            GPIO.DigitalWrite(IN4, WiringPi.High);
            GPIO.DigitalWrite(IN3, WiringPi.Low);
            GPIO.DigitalWrite(IN2, WiringPi.Low);
        }

        /// <summary>
        /// 后退
        /// </summary>
        public void Down()
        {
            GPIO.DigitalWrite(IN1, WiringPi.Low);
            GPIO.DigitalWrite(IN4, WiringPi.Low);
            GPIO.DigitalWrite(IN3, WiringPi.High);
            GPIO.DigitalWrite(IN2, WiringPi.High);
        }

        /// <summary>
        /// 左转
        /// </summary>
        public void Left()
        {
            GPIO.DigitalWrite(IN1, WiringPi.High);
            GPIO.DigitalWrite(IN4, WiringPi.Low);
            GPIO.DigitalWrite(IN3, WiringPi.High);
            GPIO.DigitalWrite(IN2, WiringPi.Low);
        }

        /// <summary>
        /// 右转
        /// </summary>
        public void Right()
        {
            GPIO.DigitalWrite(IN1, WiringPi.Low);
            GPIO.DigitalWrite(IN4, WiringPi.High);
            GPIO.DigitalWrite(IN3, WiringPi.Low);
            GPIO.DigitalWrite(IN2, WiringPi.High);
        }

        /// <summary>
        /// 修改速度
        /// </summary>
        /// <param name="values"></param>
        public void Rev(int values)
        {
            GPIO.SoftPwmWrite(ENA, values);
            GPIO.SoftPwmWrite(ENB, values);
        }

        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            GPIO.DigitalWrite(IN1, WiringPi.Low);
            GPIO.DigitalWrite(IN2, WiringPi.Low);
            GPIO.DigitalWrite(IN3, WiringPi.Low);
            GPIO.DigitalWrite(IN4, WiringPi.Low);
        }

    }
}
